package com.example.im.immsgdemo;

import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import io.rong.imkit.fragment.ConversationFragment;
import io.rong.imlib.model.Conversation;

public class ImActivity extends AppCompatActivity {

    public static final String TARGETID = "targetId";//回话id

    private TextView tvTopShow;
    private String mTargetId;
    private Conversation.ConversationType mConversationType;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_im);

        tvTopShow = findViewById(R.id.tvTopShow);


        mTargetId = getIntent().getData().getQueryParameter(TARGETID);
        mConversationType = Conversation.ConversationType.valueOf(getIntent().getData().getLastPathSegment().toUpperCase());

        String title = getIntent().getData().getQueryParameter("title");
        tvTopShow.setText(title + mTargetId);


        initSessionUi();
    }


    /**
     * 加载会话
     */
    private void initSessionUi() {
        ConversationFragment fragment = new ConversationFragment();
        Uri uri = Uri.parse("rong://" + getApplicationInfo().packageName).buildUpon()
                .appendPath("conversation")
                .appendPath(mConversationType.getName().toLowerCase())
                .appendQueryParameter("targetId", mTargetId).build();


        fragment.setUri(uri);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.rong_content, fragment);
        transaction.commitAllowingStateLoss();
    }
}
